unit SmallStruct.ActionView.TSSActionPanel;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  SmallStruct.Form.TSSFrame,
  SmallStruct.Core,
  SmallStruct.Core.TSSObjectContext,
  SmallStruct.ActionView,
  SmallStruct.Core.TSSRuntimeObject,
  SmallStruct.Core.TSSObject,
  RzBckgnd,
  ExtCtrls,
  RzPanel,
  RzTabs,
  SmallStruct.Core.TSSObjectContainer;

type
  TSSActionPanel = class(TSSFrame, ISSActionPanel)
    _PageControl: TRzPageControl;
  private
    _ActionButtonContainer: ISSObjectContainer;
    _PanelContainer: ISSObjectContainer;
  private
    procedure AppendPage(const APageCaption: string);
  public
    function GetActionButtonContainer: ISSObjectContainer;
    procedure InitObject; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

  TSSActionPanelContext = class(TSSObjectContext, ISSActionPanelContext)
  private
    _ActionConfiguration: ISSObjectContainer;
  public
    function GetActionConfiguration: ISSObjectContainer;
    procedure SetActionConfiguration(AActionConfiguration: ISSObjectContainer);
  end;

implementation

uses
  SmallStruct.Configuration,
  SmallStruct.Form.TSSPanel,
  SmallStruct.ActionView.TSSActionButton;

{$R *.dfm}

{ TSSActionPanelContext }

function TSSActionPanelContext.GetActionConfiguration: ISSObjectContainer;
begin
  Result := _ActionConfiguration;
end;

procedure TSSActionPanelContext.SetActionConfiguration(AActionConfiguration: ISSObjectContainer);
begin
  _ActionConfiguration := AActionConfiguration;
end;

{ TSSActionPanel }

procedure TSSActionPanel.AppendPage(const APageCaption: string);
var
  Page: TRzTabSheet;
  Panel: TSSPanel;
begin
  Page := TRzTabSheet.Create(Self);
  Page.Caption := APageCaption;
  Page.PageControl := _PageControl;

  Panel := TSSPanel.Create(Page);
  Panel.Parent := Page;
  Panel.Align := alClient;
  Panel.Font.Color := clGray;

  _PanelContainer.AppendObject(APageCaption, Panel);

  _PageControl.ActivePageIndex := 0;
end;

constructor TSSActionPanel.Create(AOwner: TComponent);
begin
  inherited;
  Name := '';
  _ActionButtonContainer := TSSObjectContainer.Create(Self);
  _PanelContainer := TSSObjectContainer.Create(Self);
end;

destructor TSSActionPanel.Destroy;
begin
  _ActionButtonContainer.ReleaseObjectInstance;
  _PanelContainer.ReleaseObjectInstance;

  inherited;
end;

function TSSActionPanel.GetActionButtonContainer: ISSObjectContainer;
begin
  Result := _ActionButtonContainer;
end;

procedure TSSActionPanel.InitObject;
var
  ActionConfiguration: ISSActionViewActionConfiguration;
  Index: Integer;
  Panel: TSSPanel;
  ActionButton: TSSActionButton;
begin
  inherited;

  for Index := 0 to (GetContext as ISSActionPanelContext).GetActionConfiguration.GetCount - 1 do
  begin
    ActionConfiguration := (GetContext as ISSActionPanelContext).GetActionConfiguration.GetObject(Index) as ISSActionViewActionConfiguration;

    if (_PageControl.PageCount < 1) or (_PanelContainer.FindObject(ActionConfiguration.GetActionCatalog) < 0) then
      AppendPage(ActionConfiguration.GetActionCatalog);

    Panel := _PanelContainer.GetObject(ActionConfiguration.GetActionCatalog) as TSSPanel;

    ActionButton := TSSActionButton.Create(Panel._Panel);
    ActionButton.Parent := Panel._Panel;
    //ActionButton.Visible := False;

    _ActionButtonContainer.AppendObject(ActionConfiguration.GetActionName, ActionButton);

    ActionButton.SetActionName(ActionConfiguration.GetActionName);
    ActionButton.SetActionCaption(ActionConfiguration.GetActionCaption);

    if not ActionConfiguration.IsDisplay then
      ActionButton.Visible := False;

    ActionButton.Top := (Panel.Height - 46) div 2;

    ActionButton._Button.ShowFocus := False;

    ActionButton.Left := 75 * (Panel.GetActionButtonCount - 1) + 4 * (Panel.GetActionButtonCount);
  end;
end;

end.
